package com.example.codegen;

import java.util.ArrayList;

public class LoopStatement implements Statement {
    Variables indexVar;
    Variables startVal;
    Variables endVal;
    Variables stepVal;
    public ArrayList<Statement> statements;
    // any spawn statement in this scope
    public boolean useSpawn;

    public LoopStatement(Variables indexVar, Variables startVal, Variables endVal, Variables stepVal, ArrayList<Statement> statements, boolean useSpawn) {
        this.indexVar = indexVar;
        this.startVal = startVal;
        this.endVal = endVal;
        this.stepVal = stepVal;
        this.statements = statements;
        this.useSpawn = useSpawn;
    }

    public String toJava() {
        // fix: use int
        return String.format("for(int %s = %s; %s < %s; %s += %s)", indexVar.toJava(), startVal.toJava(), indexVar.toJava(), endVal.toJava(), indexVar.toJava(), stepVal.toJava())
                + "{\n" + String.join("\n", statements.stream().map(Statement::toJava).toArray(String[]::new)) + "\n}";
    }

    @Override
    public String toGo() {
        return String.format("for %s := %s; %s < %s; %s += %s", indexVar.toGo(), startVal.toGo(), indexVar.toGo(), endVal.toGo(), indexVar.toGo(), stepVal.toGo())
                + "{\n" + String.join("\n", statements.stream().map(Statement::toGo).toArray(String[]::new)) + "\n}";
    }

    @Override
    public String toJs() {
        return String.format("for(let %s = %s; %s < %s; %s += %s)", indexVar.toJs(), startVal.toJs(), indexVar.toJs(), endVal.toJs(), indexVar.toJs(), stepVal.toJs())
                + "{\n" + String.join("\n", statements.stream().map(Statement::toJs).toArray(String[]::new)) + "\n}";
    }

    @Override
    public String toCSharp() {
        return String.format("for(int %s = %s; %s < %s; %s += %s)", indexVar.toCSharp(), startVal.toCSharp(), indexVar.toCSharp(), endVal.toCSharp(), indexVar.toCSharp(), stepVal.toCSharp())
                + "{\n" + String.join("\n", statements.stream().map(Statement::toCSharp).toArray(String[]::new)) + "\n}";
    }

    public String toCJ() {
        return String.format("for (%s in %s..%s:%s)", indexVar.toCJ(), startVal.toCJ(), endVal.toCJ(), stepVal.toCJ())
                + "{\n\t" + String.join("\n\t\t", statements.stream().map(Statement::toCJ).toArray(String[]::new)) + "\n\t}";
    }
}
